package weaver.interfaces.zhlx.duss.action.salesmodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 客户主数据变更流程结束调用SAP客户主数据接口
 * @ModifyLog:
 **/
public class WeaKhzsjbglcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaKhzsjbglcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil saptil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONObject dataJson = new JSONObject();
            JSONObject dataheaderJson = new JSONObject();
            dataheaderJson.put("KTOKD", Util.null2String(rs.getString("khz")));
            dataheaderJson.put("KUNNR", Util.null2String(rs.getString("khbh")));
            dataheaderJson.put("NAME1", Util.null2String(rs.getString("khmc1")));
            dataheaderJson.put("NAME2", Util.null2String(rs.getString("khmc2")));
            dataheaderJson.put("BU_SORT1", Util.null2String(rs.getString("khssc1")));
            dataheaderJson.put("BU_SORT2", Util.null2String(rs.getString("khywjc")));
            dataheaderJson.put("TITLE_LET", Util.null2String(rs.getString("wh")));
            dataheaderJson.put("VBUND", Util.null2String(rs.getString("myhb")));
            JSONObject creditJson = new JSONObject();
            creditJson.put("UKM_CREDIT_SGMNT", Util.null2String(rs.getString("xyd")));
            creditJson.put("CREDIT_LIMIT", Util.null2String(rs.getString("xyed")));
            creditJson.put("LIMIT_VALID_DATE", Util.null2String(rs.getString("yxqz")).replace("-", ""));
            dataJson.put("CREDIT", creditJson);
            rs.executeQuery("select * from " + tablename + "_dt3 where mainid=?", mainid);
            rs.next();
            dataheaderJson.put("LAND1", Util.null2String(rs.getString("gjbm")));
            dataheaderJson.put("ORT02", Util.null2String(rs.getString("dqbm")));
            dataheaderJson.put("PSTLZ", Util.null2String(rs.getString("yzbm")));
            dataheaderJson.put("ORT01", Util.null2String(rs.getString("cs")));
            dataheaderJson.put("STRAS", Util.null2String(rs.getString("jdq")));
            dataheaderJson.put("STR_SUPPL2", "");
            dataheaderJson.put("STR_SUPPL3", "");
            dataheaderJson.put("SPRAS", Util.null2String(rs.getString("yydm")));
            dataheaderJson.put("TELF1", Util.null2String(rs.getString("dh")));
            dataheaderJson.put("TELF2", Util.null2String(rs.getString("yddh")));
            dataheaderJson.put("TELFX", Util.null2String(rs.getString("cz")));
            dataheaderJson.put("SMTP_ADDR", Util.null2String(rs.getString("email")));
            dataheaderJson.put("IDTYPE", Util.null2String(rs.getString("bslx")));
            dataheaderJson.put("IDNUMBER", Util.null2String(rs.getString("bsbh")));
            dataJson.put("DATAHEADER", dataheaderJson);
            JSONArray bukrsitem = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject bukrsitemJson = new JSONObject();
                bukrsitemJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
                bukrsitemJson.put("AKONT", Util.null2String(rs.getString("tykm")));
                bukrsitemJson.put("KNRZE", Util.null2String(rs.getString("zb")));
                bukrsitemJson.put("ZTERM", Util.null2String(rs.getString("fktjbm")));
                String fkfs = Util.null2String(rs.getString("fkfs"));
                String[] arr = fkfs.split(",");
                String fkfswb = "";
                for (String str : arr) {
                    if ("0".equals(str)) {
                        fkfswb += "X";
                    }
                    if ("1".equals(str)) {
                        fkfswb += "C";
                    }
                    if ("2".equals(str)) {
                        fkfswb += "T";
                    }
                    if ("3".equals(str)) {
                        fkfswb += "B";
                    }
                    if ("4".equals(str)) {
                        fkfswb += "Z";
                    }
                }
                bukrsitemJson.put("ZWELS", fkfswb);
                bukrsitem.add(bukrsitemJson);
            }
            dataJson.put("BUKRSITEM", bukrsitem);

            JSONArray saleitem = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject saleitemJson = new JSONObject();
                saleitemJson.put("VKORG", Util.null2String(rs.getString("xszz")));
                saleitemJson.put("VTWEG", Util.null2String(rs.getString("fxqdbm")));
                saleitemJson.put("SPART", Util.null2String(rs.getString("cpzbm")));
                saleitemJson.put("BZIRK", Util.null2String(rs.getString("xsdqbm")));
                saleitemJson.put("EIKTO", Util.null2String(rs.getString("khcwfzh")));
                saleitemJson.put("WAERS", Util.null2String(rs.getString("bzbm")));
                saleitemJson.put("INCO1", Util.null2String(rs.getString("gjmytkbm")));
                saleitemJson.put("INCO2", Util.null2String(rs.getString("gjmytkdd")));
                saleitemJson.put("ZTERM", Util.null2String(rs.getString("fktjbm")));
                saleitem.add(saleitemJson);
            }
            dataJson.put("SALEITEM", saleitem);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaKhjdlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaKhzsjbglcAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = saptil.handleOKHttpRequest("SD055", dataJson, dataMap);
            writeLog(">>>>>WeaKhzsjbglcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String dataStr = outputJson.getString("DATA");
                JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                if (!"S".equals(msgType)) {
                    for (int i = 0; i < returnList.size(); i++) {
                        String msgType1 = returnList.getJSONObject(i).getString("MSG_TYPE");
                        if ("E".equals(msgType1)) {
                            message += returnList.getJSONObject(i).getString("MSG_TEXT")+";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaKhzsjbglcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}